package com.ruoyi.xiaoshou.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.xiaoshou.dto.AddOrderDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.xiaoshou.domain.SaleRequest;
import com.ruoyi.xiaoshou.service.ISaleRequestService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 销售申请Controller
 * 
 * @author ruoyi
 * @date 2024-10-26
 */
@RestController
@Slf4j
@RequestMapping("/saleRequest")
public class SaleRequestController extends BaseController
{
    @Autowired
    private ISaleRequestService saleRequestService;

    /**
     * 查询销售申请列表
     */
    @RequiresPermissions("xiaoshou:saleRequest:list")
    @GetMapping("/list")
    public TableDataInfo list(SaleRequest saleRequest)
    {
        startPage();
        List<SaleRequest> list = saleRequestService.selectSaleRequestList(saleRequest);
        return getDataTable(list);
    }

    /**
     * 导出销售申请列表
     */
    @RequiresPermissions("xiaoshou:saleRequest:export")
    @Log(title = "销售申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SaleRequest saleRequest)
    {
        List<SaleRequest> list = saleRequestService.selectSaleRequestList(saleRequest);
        ExcelUtil<SaleRequest> util = new ExcelUtil<SaleRequest>(SaleRequest.class);
        util.exportExcel(response, list, "销售申请数据");
    }

    /**
     * 获取销售申请详细信息
     */
    @RequiresPermissions("xiaoshou:saleRequest:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(saleRequestService.selectSaleRequestById(id));
    }

    @RequiresPermissions("xiaoshou:saleRequest:query")
    @GetMapping(value="getReqByCode/{code}")
    public AjaxResult getReqByCode(@PathVariable("code") String code)
    {
        return success(saleRequestService.getReqByCode(code));
    }

    @RequiresPermissions("xiaoshou:saleRequest:getAllRequest")
    @RequestMapping("getAllRequest")
    public AjaxResult getAllRequest()
    {
        return saleRequestService.getAllRequest();
    }

    /**
     * 新增销售申请
     */
    @RequiresPermissions("xiaoshou:saleRequest:add")
    @Log(title = "销售申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SaleRequest saleRequest)
    {
        log.info("SaleRequestController  add  start: "+saleRequest);
        return toAjax(saleRequestService.insertSaleRequest(saleRequest));
    }

    @RequiresPermissions("xiaoshou:saleRequest:add")
    @PostMapping("addOrder")
    public AjaxResult addOrder(@RequestBody AddOrderDto addOrderDto)
    {
        log.info("SaleRequestController  addOrder  start: "+addOrderDto);
        return saleRequestService.addOrder(addOrderDto);
    }

    /**
     * 修改销售申请
     */
    @RequiresPermissions("xiaoshou:saleRequest:edit")
    @Log(title = "销售申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SaleRequest saleRequest)
    {
        log.info("SaleRequestController  edit  start: "+saleRequest);
        return toAjax(saleRequestService.updateSaleRequest(saleRequest));
    }

    /**
     * 删除销售申请
     */
    @RequiresPermissions("xiaoshou:saleRequest:remove")
    @Log(title = "销售申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        log.info("SaleRequestController  remove  start: "+ids);
        return toAjax(saleRequestService.deleteSaleRequestByIds(ids));
    }
    @PutMapping("updSaleReq")
    public AjaxResult updSaleReq(@RequestBody SaleRequest saleRequest){
        log.info("SaleRequestController  updSaleReq  start: "+saleRequest);
        return toAjax(saleRequestService.updSaleReq(saleRequest));
    }
}
